package com.bw.a2209clubshop.ui.login.repository

import android.util.Log
import com.bawei.a2209_mianshi.utils.GSON
import com.bw.a2209clubshop.http.ClubService
import com.bw.a2209clubshop.ui.login.model.LoginMode
import com.bw.a2209clubshop.ui.login.model.LoginParam
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import javax.inject.Inject

class LoginRepository@Inject constructor() {
    @Inject
    lateinit var apiService: ClubService

    fun getLoginSmsCode(phone: String) = flow {
        delay(1_000)
        emit(apiService.getSmsCode(phone))
        Log.d("SMS_CODE", apiService.getSmsCode(phone).msg)
    }
//    "grantType": "sms",
//    "newPassword": "",
//    "operatorType": 2,
//    "password": "Yjk825369",
//    "smsCode": 778545,
//    "userName": 14796812519
    fun login(
        mode: LoginMode,
        phone: String = "",
        code: String = "",
        password: String = "",
        operatorType:Int=2,
        newPassword:String=""
    ) = flow {
        val postModel = LoginParam(mode.value,newPassword,2,password,code.toInt(),phone.toLong())
//        val body = RequestBody.create("application/json".toMediaType(), GSON.toJson(postModel))
        emit(apiService.login(postModel))
    }
//    fun getRestSmsCode(phone: String) = flow {
//        delay(1_000)
//        emit(apiService.getRestSmsCode(phone))
//    }
//
//    fun getBindSmsCode(phone: String) = flow {
//        delay(1_000)
//        emit(apiService.getBindSmsCode(phone))
//    }
}